#ifndef TEXTURE_H
#define TEXTURE_H
#pragma once
#include <IL\il.h>
#include <IL\ilu.h>
#include <glut.h>
enum TexType {DUCK,GOOSE,HEN,EGG,BASKET,SHIT,SIGHT};

typedef struct TextureImage
{
    GLubyte *imageData;
    GLuint bpp;
    GLuint width,height;
    GLuint texID;
    
} TextureImage;

class ClassTexture
{
    public:
        
        ClassTexture()
        {
            ilInit();
            iluInit();
        }
        
        void LoadTexture(ILenum FileType, char * filename, TextureImage * texture)
        {
            ilLoad(FileType, (const wchar_t *)filename);
            
            texture -> width = ilGetInteger(IL_IMAGE_WIDTH);
            texture -> height = ilGetInteger(IL_IMAGE_HEIGHT);
            texture -> bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
            
            texture -> imageData = ilGetData();
            
            ilEnable(IL_CONV_PAL);
            
            unsigned int type = ilGetInteger(IL_IMAGE_FORMAT);
            
            glGenTextures(1, &texture -> texID);
            glBindTexture(GL_TEXTURE_2D, texture -> texID);
            
            gluBuild2DMipmaps(GL_TEXTURE_2D, texture -> bpp, texture -> width,
            texture -> height, type, GL_UNSIGNED_BYTE, texture -> imageData);
            
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        }
        
} *Texture;

TextureImage Textures[7];

void loadTextures()
{
    Texture = new ClassTexture;
	Texture -> LoadTexture(IL_PNG, "Textures/vit.png", &Textures[DUCK]);
	Texture -> LoadTexture(IL_PNG, "Textures/ga.png", &Textures[HEN]);
	Texture -> LoadTexture(IL_PNG, "Textures/ngan.png", &Textures[GOOSE]);
	Texture -> LoadTexture(IL_PNG, "Textures/egg.png", &Textures[EGG]);
	Texture -> LoadTexture(IL_PNG, "Textures/Ro.png", &Textures[BASKET]);
	Texture -> LoadTexture(IL_PNG, "Textures/shit.png", &Textures[SHIT]);
	Texture -> LoadTexture(IL_PNG, "Textures/SIGHT.png", &Textures[SIGHT]);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
}

void DrawTexture(float x, float y, int type, float width = -1, float height = -1)
{
	glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, Textures[type].texID);

	if (width == -1 || height == -1)
	{
		width = Textures[type].width;
		height = Textures[type].height;
	}

    glBegin(GL_QUADS);
    glTexCoord2f(0,1); glVertex3f(x + -width / 2, y + -height / 2, 0);
    glTexCoord2f(1,1); glVertex3f(x + width / 2, y + -height / 2, 0);
    glTexCoord2f(1,0); glVertex3f(x + width / 2, y + height / 2, 0);
    glTexCoord2f(0,0); glVertex3f(x + -width / 2, y + height / 2, 0);
    glEnd();
	glFlush();
	glDisable(GL_TEXTURE_2D);
}
#endif